package edu.guet.server.controller;

import edu.guet.server.model.Course;
import edu.guet.server.model.SemesterStatistic.SemesterPermissions;
import edu.guet.server.model.dto.AddSemester;
import edu.guet.server.model.Resource;
import edu.guet.server.model.Semester;
import edu.guet.server.model.dto.SemesterIntro;
import edu.guet.server.model.dto.ShowSemesterDTO;
import edu.guet.server.model.vo.NoticeVO;
import edu.guet.server.service.*;
import edu.guet.server.utils.AliOSSUtils;
import edu.guet.server.utils.CompareCurTime;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/semesters")
public class SemesterController {
    @Autowired
    ResourceService resourceService;
    @Autowired
    SemesterService semesterService;
    @Autowired
    TechSemService techSemService;
    @Autowired
    StuSemService stuSemService;
    @Autowired
    UnitService unitService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private CourseService courseService;

    /**
     * 添加课基本信息
     *
     * @param addsemester （进来的是没有semester_id的
     * @return
     */
    //&&
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/addsemester")
    ResultAPI addSemester(@RequestBody AddSemester addsemester) {
        log.info("添加新课程内容: {}", addsemester);
        Integer semesterId = semesterService.addSemester(addsemester);
        //返回学期课程id
        addsemester.setSemester_id(semesterId);
        log.info("成功添加新课程");
        return ResultAPI.success(addsemester, "添加课程并回显成功!");
    }
    @DeleteMapping("/{semester_id}")
    ResultAPI deleteSemester(@PathVariable Integer semester_id) {
        semesterService.deleteSemById(semester_id);
        return ResultAPI.success();
    }



    /**
     * 复制课程并回显到输入框
     *
     * @return
     */
    @PostMapping("/copysemester")
    ResultAPI copySemester(@RequestBody Semester semester) {
        //课程码搜索获取课堂
        semesterService.copySem(semester);
        return ResultAPI.success();//返回
    }

//    /**
//     * 修改课程基本信息（开课了就不能改了
//     *
//     * @param addsemester（进来的是是有semesterId的
//     * @return
//     */
//    @Transactional(rollbackFor = Exception.class)
//    @PutMapping("/updatesemester")
//    ResultAPI updateSemester(@RequestBody AddSemester addsemester) {
//        log.info("修改课程内容: {}", addsemester);
//        //更新修改信息
//        Semester semester = new Semester();
//        semester.copyAddSemester(addsemester);
//        semesterService.updateSemester(semester);
//        return ResultAPI.success(addsemester, "修改成功！");
//    }

    /**
     * 获取课程介绍（基本信息
     *
     * @param semester_id
     * @return
     */
    //&&
//    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/showbase")
    ResultAPI showSemester(@RequestParam Integer semester_id) {
        log.info("传入的semester_id:{}", semester_id);
        SemesterIntro semesterIntro = new SemesterIntro();
        //单元数
        Integer unit_cnt = unitService.countUnit(semester_id);
        //视频数
        Integer video_cnt = resourceService.getTypeCountBySemesterId("video", semester_id);
        //课件数
        Integer ppt_cnt = resourceService.getTypeCountBySemesterId("ppt", semester_id);
        //课堂Semester
        Semester semester = semesterService.findSemesterById(semester_id);
        //课程id
        Integer courseId = semester.getCourse_id();
        Course course=courseService.getCourseByCourseId(courseId);
        String courseIntro=course.getCourse_intro();
        Integer resourceId=course.getResource_id();
        //根据reId查找封面
        String url = resourceService.getResourceById(resourceId).getResource_url();
        semesterIntro.setResource_url(url);
        semesterIntro.setUnit_cnt(unit_cnt);
        semesterIntro.setPpt_cnt(ppt_cnt);
        semesterIntro.setVideo_cnt(video_cnt);
        semesterIntro.setSemester_intro(courseIntro);
        semesterIntro.setSemester_name(semester.getSemester_name());
        semesterIntro.setStu_cnt(semester.getStu_cnt());
        semesterIntro.setStart_time(semester.getStart_time());
        semesterIntro.setEnd_time(semester.getEnd_time());
        semesterIntro.setSemester_id(semester_id);

        //判断是否开始
        if(!CompareCurTime.compareTime(semesterIntro.getStart_time())){
            //开始课程
            semesterService.updateSemStatus(semesterIntro.getSemester_id(),1);
            semesterIntro.setSemester_status(1);
        }
        //判断是否过期
        if(!CompareCurTime.compareTime(semesterIntro.getEnd_time())){
            //关闭课程
            semesterService.updateSemStatus(semesterIntro.getSemester_id(),0);
            semesterIntro.setSemester_status(0);
        }
        //公告
        NoticeVO noticeVO = noticeService.lastnotice(semester_id);
        if(noticeVO!=null){
            semesterIntro.setNotice_title(noticeVO.getNotice_title());
            semesterIntro.setNotice_content(noticeVO.getNotice_content());
        }
        return ResultAPI.success(semesterIntro, "请求成功！");

    }

    /**
     * 展示老师课程列表
     *
     * @param tech_id
     * @return
     */
    //&&
    @GetMapping("/showlist")
    ResultAPI showList( @RequestParam Integer tech_id) {
        if (tech_id == null) {
            return ResultAPI.error("老师id为空");
        }
        log.info("老师id：{}",tech_id);
        List<ShowSemesterDTO> showList ;
        //获得老师相关course
        Integer reId=courseService.findCourseByTechId(tech_id).getResource_id();
        String resourceUrl=resourceService.getResourceById(reId).getResource_url();
        //获得老师关联的课程的所有id
        List<Integer> semIds = techSemService.findsemesterIdsBytechId(tech_id);
        //批量搜一步到位
        log.info("找到的课程id{}",semIds.toString());
        if(semIds!=null&&semIds.size()>0){
            showList=semesterService.findSemesterByIds(semIds,null);
            //搜索视频封面
            for(ShowSemesterDTO item:showList){
                item.setResource_url(resourceUrl);
            }
            return ResultAPI.success(showList,"请求成功！");
        }
        return ResultAPI.success("请创建课堂！");
    }

    @GetMapping("/getPermissions")
    ResultAPI getPermissions(@RequestParam Integer semester_id) {
        SemesterPermissions semesterPermissions = semesterService.findPermissionsById(semester_id);
        if(semesterPermissions!=null){
            return ResultAPI.success(semesterPermissions);
        }else return ResultAPI.success("请添加权重！（100%）");
    }



}
